package com.example.myapplication.ui.activity.collect

import android.app.Activity
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.bean.collect.WebsiteCollectBean
import com.kun.base.ui.AbsBaseActivity
import com.kun.base.util.ToastUtils
import kotlinx.android.synthetic.main.activity_add_collection.*
import kotlinx.android.synthetic.main.toolbar_layout.*

/**
 *新增我的收藏
 *
 * @author : Kun
 * @date : 2021/6/8
 * @time : 16:31
 */
@Route(path = ARouterPath.ADD_COLLECTION)
class AddCollectionActivity : AbsBaseActivity() {
    /**
     * 类型 0新增文章收藏，
     *  1新增/修改网站收藏
     */
    @JvmField
    @Autowired
    var type: Int = 0

    /**
     * 修改网站收藏传入的bean
     */
    @JvmField
    @Autowired
    var websiteCollectBean: WebsiteCollectBean? = null

    private lateinit var addCollectionViewModel: AddCollectionViewModel
    override fun getLayoutId(): Int {
        return R.layout.activity_add_collection
    }

    override fun initViewModel() {
        addCollectionViewModel = getActivityScopeViewModel(AddCollectionViewModel::class.java)
        addCollectionViewModel.collectRequest.addOtherArticleRequest.observe(this, Observer { t ->
            //新增收藏文章
            if (t.errorCode == 0) {
                setResult(Activity.RESULT_OK)
                finish()
            }
        })

        addCollectionViewModel.collectRequest.addWebsiteResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                setResult(Activity.RESULT_OK)
                finish()
            }
        })
        addCollectionViewModel.collectRequest.editWebsiteResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                setResult(Activity.RESULT_OK)
                finish()
            }
        })
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        iv_back.setOnClickListener { finish() }
        if (type == 0) {
            group_author.visibility = View.VISIBLE
            tv_title.text = "收藏文章"
        } else {
            group_author.visibility = View.GONE
            tv_title.text = if (websiteCollectBean == null) "新增网站" else "修改网站"
            if (websiteCollectBean != null) {
                et_title.setText(websiteCollectBean!!.name)
                et_url.setText(websiteCollectBean!!.link)
            }
        }
        tv_right.text = "保存"
        tv_right.visibility = View.VISIBLE
        tv_right.setOnClickListener {
            submit()
        }
    }

    /**
     * 提交
     */
    private fun submit() {
        val title = et_title.text.toString()
        val author = et_author.text.toString()
        val url = et_url.text.toString()
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShortToast("请输入标题")
            return
        }
        if (group_author.visibility == View.VISIBLE && TextUtils.isEmpty(author)) {
            ToastUtils.showShortToast("请输入文章作者")
            return
        }
        if (TextUtils.isEmpty(url)) {
            ToastUtils.showShortToast("请输入链接地址")
            return
        }
        if (type == 0) {
            addCollectionViewModel.requestAddOtherArticle(title, author, url)
        } else {
            if (websiteCollectBean == null) {
                addCollectionViewModel.requestAddWebsite(title, url)
            } else {
                addCollectionViewModel.requestEditWebsite(websiteCollectBean!!.id, title, url)
            }
        }
    }
}